package com.backend.mapper;


import com.backend.pojo.domain.User;
import com.backend.pojo.dto.LeaveRequestDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    // 1 查询所有
    @Select("select * from user")
    List<User> selectAll();

    // 2 根据userId查询
    @Select("select * from user where userId = #{userId}")
    User selectById(String userId);

    // 3 多条件模糊查询
    @Select({
            "<script>",
            "SELECT * FROM user WHERE 1=1 ",
            "<if test='userId != null'> AND userId = #{userId} </if>",
            "<if test='userName != null and userName != \"\"'> AND userName LIKE CONCAT('%', #{userName}, '%') </if>",
            "<if test='sort != null and sort != \"\"'> AND sort LIKE CONCAT('%', #{sort}, '%') </if>",
            "</script>"
    })
    List<User> selectByConditionMulti(User user);

    // 4 多条件中选择一个条件查询
    @Select({
            "<script>",
            "SELECT * FROM user",
            "<where>",
            "<choose>",
            "<when test='userId != null'> userId = #{userId} </when>",
            "<when test='userName != null and userName != \"\"'> userName LIKE CONCAT('%', #{userName}, '%') </when>",
            "<when test='sort != null and sort != \"\"'> sort LIKE CONCAT('%', #{sort}, '%') </when>",
            "<otherwise> 1 = 1 </otherwise>",
            "</choose>",
            "</where>",
            "</script>"
    })
    List<User> selectByConditionSingle(User user);

    // 5 增加
    @Insert("insert into user(userId,password,userName,mail,sort,phone,address,birthday) " +
            "values(#{userId},#{password},#{userName},#{mail},#{sort},#{phone},#{address},#{birthday})")
    void addUser(User user);

    // 6 根据userId修改
    @Update({
            "<script>",
            "UPDATE user",
            "<set>",
            "<if test='userName != null and userName != \"\"'> userName = #{userName}, </if>",
            "<if test='password != null and password != \"\"'> password = #{password}, </if>",
            "<if test='mail != null and mail != \"\"'> mail = #{mail}, </if>",
            "<if test='sort != null and sort != \"\"'> sort = #{sort}, </if>",
            "<if test='phone != null and phone != \"\"'> phone = #{phone}, </if>",
            "<if test='address != null and address != \"\"'> address = #{address}, </if>",
            "<if test='birthday != null'> birthday = #{birthday}, </if>",
            "</set>",
            "WHERE userId = #{userId}",
            "</script>"
    })
    void update(User user);

    // 7 根据userId删除一个
    @Delete("delete from user where userId = #{userId}")
    void deleteUserById(String userId);

    // 8 根据int[] userIds删除多个
    @Delete({
            "<script>",
            "DELETE FROM user WHERE userId IN (<foreach collection='userIds' item='userId' separator=','> #{userId} </foreach>)",
            "</script>"
    })
    void deleteManyUserByIds(@Param("userIds") String[] userIds);

    @Insert("insert into sms.leave_requests (user_id, lschool, lcity, addr, contact, type, date1, date2, descp, tel, teacherName,user_name, status) VALUE(#{userId},#{lschool},#{lcity},#{addr},#{contact},#{type},#{date1},#{date2},#{desc},#{tel},#{teacherName},#{userName},#{status})")
    void leave(LeaveRequestDTO leaveRequestDTO);

}
